var App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
        card_id:"",
        info:{},
        showPopup:false,
        p_title:"",
        p_number:'',
        p_notes:'',
        issubmitting:false, //提交状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if(options.cid != undefined){
      this.setData({
        card_id:options.cid
      })
      this.getInfo()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //检测是否登录
    App.checkIsLogin()
  },
  // 获取用户信息
  getInfo(){
    let that = this;
    let obj = {card_id:that.data.card_id}
    App.request('/Manage/Management/cardsInfo',obj,function(e){
      wx.stopPullDownRefresh()
      if (e.code == "200") {
        that.setData({
            info:e.result
        })
      } 
    })
  },
  // 拨打电话
  tel() {
    let _this = this;
    wx.makePhoneCall({
      phoneNumber: _this.data.info.phone, //此号码仅用于测试 。
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  //黑名单
  switchChange:function(e){
    let obj={
      card_id:e.currentTarget.dataset.id,
      status:e.detail.value==true?1:0
    }
    App.request('/Manage/Management/blackSwitch',obj,function(res){
        if (res.code == "200") {
          wx.showToast({ title:res.msg,icon: 'success',duration: 2000})    
        } 
    })
  },
  // 页面跳转
  to_url(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  // 弹窗
  onShowPopup(e){
    let p_title = ""
    let that = this;
    if(that.data.showPopup == false){
      p_title = e.currentTarget.dataset.title
    }
    that.setData({
      showPopup: !that.data.showPopup,
      p_title,
      p_number:"",
      p_notes:'',
      issubmitting:false
    }); 
  },
  //提交
  formSubmit(e){
      let that =  this
      if(that.data.issubmitting) return;//防止重复提交
      // 设置为正在提交状态
      that.setData({issubmitting:true});
      let data =e.detail.value
      let obj = {
        card_id:that.data.info.card_id,
        nums:data.p_number,
        remark:data.p_notes,
        type:that.data.p_title=="赠费"?1:2
      }
      App.request('/Manage/Management/balanceGiveDeduct',obj,function(e){
          if (e.code == "200") {
            App.showSuccessToast(e.msg, function() {
              that.getInfo()          //刷新
              that.onShowPopup()
            });
          } else{
            // 设置为可以提交的状态
            that.setData({issubmitting:false});
            App.showError(e.msg);
          }
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getInfo()          //刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})